/*
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: hxf <hewenfei@kylinos.cn>
 *
 */

import QtQuick 2.15
import QtQml.Models 2.12
import QtQuick.Layouts 1.12

import org.ukui.menu.core 1.0
import org.ukui.menu.extension 1.0
import "../extensions" as Extension
import AppControls2 1.0 as AppControls2

import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0 as Platform

ListView {
    id: root
    signal openFolderSignal(string folderId, string folderName, int x, int y)
    signal contentShowFinished()
    property bool isContentShow

    property int itemHeight: 40
    property alias sourceModel: appGroupModel.sourceModel
    // TODO: 动态计算尺寸
    property int column: 10

    spacing: 5
    clip: true
    boundsBehavior: Flickable.StopAtBounds

    model: AppGroupModel {
        id: appGroupModel
    }

    delegate: Item {
        width: ListView.view.width
        height: childrenRect.height

        Column {
            width: parent.width
            height: childrenRect.height
            spacing: 0

            AppControls2.LabelItem {
                width: parent.width
                height: root.itemHeight
                displayName: model.name
                visible: displayName !== ""
            }

            GridView {
                width: parent.width
                height: childrenRect.height
                cellWidth: width / root.column
                cellHeight: cellWidth
                interactive: false

                model: DelegateModel {
                    model: appGroupModel
                    rootIndex: modelIndex(index)
                    delegate: Item {
                        width: GridView.view.cellWidth
                        height: GridView.view.cellHeight

                        FullScreenAppItem {
                            anchors.fill: parent
                            anchors.margins: 12

                            acceptedButtons: Qt.LeftButton | Qt.RightButton

                            text.text: model.name
                            icon.source: model.icon

                            onClicked: (event) => {
                                if (event.button === Qt.LeftButton) {
                                    // openApplication
                                    appManager.launchApp(id);
                                } else if (mouse.button === Qt.RightButton) {
                                    // appListView.model.openMenu(index, MenuInfo.FullScreen);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    header: Item {
        width: root.width
        height: childrenRect.height

        property var widgets: []
        property var widgetInfos: []
        property int widgetCount: 1

        Component.onCompleted: {
            widgetInfos.push({label: "favorite", display: "non-starred-symbolic", type: WidgetMetadata.Widget});
            widgets.push("favorite");
        }

        Column {
            width: parent.width
            height: childrenRect.height + spacing
            spacing: root.spacing

            AppControls2.LabelItem {
                width: parent.width
                height: root.itemHeight
                displayName: qsTr("Favorite")
            }

            GridView {
                id: favoriteView
                width: parent.width
                height: contentHeight
                property string mergeToAppId: ""
                property bool isMergeToFolder: false
                property bool dragTypeIsMerge: false
                property int exchangedStartIndex: 0
                property alias viewModel: visualModel

                cellWidth: width / root.column
                cellHeight: cellWidth
                model: DelegateModel {
                    id: visualModel
                    model: favoriteModel
                    delegate: Item {
                        id: container
                        width: favoriteView.cellWidth
                        height: favoriteView.cellHeight
                        Extension.FavoriteDelegate {
                            anchors.fill: parent
                            anchors.margins: 12

                            visualIndex: container.DelegateModel.itemsIndex
                            delegateLayout.anchors.topMargin: 16
                            delegateLayout.anchors.bottomMargin: 16
                            delegateLayout.spacing: 8
                            mergePrompt.anchors.topMargin: 10
                            mergePrompt.width: width*0.67
                            mergePrompt.radius: 32

                            Component.onCompleted: contentShowFinished.connect(resetOpacity)
                            Component.onDestruction: contentShowFinished.disconnect(resetOpacity)
                        }
                    }
                }

                displaced: Transition {
                    NumberAnimation { properties: "x,y"; easing.type: Easing.OutQuad; duration: 200 }
                }
            }
        }
    }
}
